package com.innoking.docsys.workflow.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.innoking.docsys.utils.OtherPojo;
import com.innoking.docsys.utils.ResultObjVO;
import com.innoking.docsys.utils.ResultUtil;
import com.innoking.docsys.workflow.feign.UserRestInterface;
import com.innoking.docsys.workflow.module.WorkflowGroupVO;
import com.innoking.docsys.workflow.module.WorkflowUserVO;
import com.innoking.docsys.workflow.service.WorkflowIdentityService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.GroupQuery;
import org.activiti.engine.identity.User;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述:
 *
 * @author zyp
 * @description 工作流用户与组service实现类
 * @create 2019-09-06 13:45
 */
@Service
public class WorkflowIdentityServiceImpl implements WorkflowIdentityService {

    Logger logger= LoggerFactory.getLogger(WorkflowIdentityServiceImpl.class);

    @Autowired
    IdentityService identityService;

//    @Autowired
//    UserRestInterface userRestInterface;

    /**
     * @author:zyp
     * @description 添加和更新用户
     * @date: 2019/9/6 13:50
     * @return
     */
    @Override
    public ResultObjVO addUpdateUser(WorkflowUserVO workflowUser) {
        User user = identityService.createUserQuery().userId(workflowUser.getId()).singleResult();
        if(user==null){
            user = identityService.newUser(workflowUser.getId());
        }
        user.setFirstName(workflowUser.getFirstName());
        user.setLastName(workflowUser.getLastName());
        user.setEmail(workflowUser.getEmail());
        if (StringUtils.isNotBlank(workflowUser.getPassword())) {
            user.setPassword(workflowUser.getPassword());
        }
        identityService.saveUser(user);
        //在新增或修改时，同时修改当前用户所在的组（角色、组织机构）
        //在之前先删除其所在的用户、组
        GroupQuery groupQuery = identityService.createGroupQuery();
        groupQuery.groupMember(workflowUser.getId());
        List<Group> list = groupQuery.list();
        for (Group group:list){
            identityService.deleteMembership(workflowUser.getId(), group.getId());
        }
        OtherPojo pojo=new OtherPojo();
        pojo.setToken(null);
        pojo.setDeviceId(null);
        pojo.setObj(workflowUser.getId());
        String jsonstr= JSONObject.toJSON(pojo).toString();
        //根据用户查询其组织机构以及角色
//        String deptByUserId = userRestInterface.getDeptByUserId(jsonstr);
//        String roleById = userRestInterface.getRoleByUserId(jsonstr);
        /**
         * 未完...待开发...
         */
        return ResultUtil.success( "成功" );
    }

    /**
     * @author:zyp
     * @description 删除用户
     * @date: 2019/9/6 13:53
     * @return 
     */
    @Override
    public ResultObjVO deleteUser(List<String> ids) {
            for (String id : ids) {
                if (StringUtils.isBlank(id)) {
                    continue;
                }
                identityService.deleteUser(id);
            }
        return ResultUtil.success( "删除成功" );
    }

    /**
     * @author:zyp
     * @description 保存/更新组
     * @date: 2019/9/6 13:56
     * @return 
     */
    @Override
    public ResultObjVO saveUpdateGroup(WorkflowGroupVO workflowGroup) {
        Group group = identityService.createGroupQuery().groupId(workflowGroup.getId()).singleResult();
        if (group == null) {
            group = identityService.newGroup(workflowGroup.getId());
        }
        group.setName(workflowGroup.getName());
        group.setType(workflowGroup.getType());
        identityService.saveGroup(group);
        return ResultUtil.success( "成功" );
    }

    /**
     * @author:zyp
     * @description 删除工作流组
     * @date: 2019/9/6 13:58
     * @return 
     */
    @Override
    public ResultObjVO deleteGroup(String ids) {
        if (!StringUtils.isBlank(ids)) {
            String[] idArray = ids.split("-");
            for (String id : idArray) {
                if (StringUtils.isBlank(id)) {
                    continue;
                }
                identityService.deleteGroup(id);
            }
        }
        return ResultUtil.success( "删除成功" );
    }

}